home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / docs / linux-do / programm / lpg-0.4 / lpg-0 / LPG / examples / screen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-17  |  4.6 KB  |  198 lines

  1. /*
  2.  * Screen.C - A demonstration program for ncurses.
  3.  *            This program is originally distributed as a part of 
  4.  *            the <Linux Programmers Guide>
  5.  * 
  6.  *  AUTHOR: Sven van der Meer (vdmeer@cs.tu-berlin.de)
  7.  *
  8.  *  This program is free software; you can redistribute it and/or
  9.  *  modify it under the terms of the GNU General Public License as
  10.  *  published by the Free Software Foundation; either version 2 of
  11.  *  the License,or (at your option) any later version.
  12.  *
  13.  *  This program is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with this program; if not, write to the Free Software
  20.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. #include <ncurses/ncurses.h>
  25. #include <signal.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28.  
  29.  
  30. void init_screen();
  31. void end_screen();
  32. void vdmerase(WINDOW *);
  33. void usage();
  34. void winsizech();
  35.  
  36. WINDOW *leftwin;
  37. WINDOW *rightwin;
  38. int todo;
  39.  
  40. void main (int argc, char *argv[])
  41. {
  42.  if(strcmp(argv[1],NULL)>=0){
  43.    usage();
  44.  }
  45.  todo=atoi(argv[1]);
  46.  signal(SIGWINCH,winsizech);
  47.  
  48.  init_screen();
  49.  
  50.  while(!getch())
  51.    ;
  52.  
  53.  end_screen();
  54. exit(0);
  55. }
  56.  
  57.  
  58.  
  59. void init_screen()
  60. {
  61. int mycols=0, myrows=0;
  62. int leftcol=0, leftrow=0, rightcol=0, rightrow=0;
  63.  
  64.  if(!(stdscr=initscr())){
  65.    fprintf(stderr,"type: initscr() failed\n\n");
  66.    exit(1);
  67.  }
  68.  
  69.  switch(todo){
  70.    case 1:
  71.             myrows=22;
  72.             mycols=77;
  73.             leftrow=((LINES-myrows)/2);
  74.             leftcol=((COLS-mycols)/2);
  75.             rightrow=leftrow;
  76.             rightcol=leftcol;
  77.             break;
  78.    case 2:
  79.             myrows=LINES-2;
  80.             mycols=(COLS-6)/2;
  81.             leftrow=1;
  82.             leftcol=2;
  83.             rightrow=1;
  84.             rightcol=4+mycols;
  85.             break;
  86.    case 3:
  87.             myrows=(LINES/2+LINES/4);
  88.             mycols=(COLS/2+COLS/4);
  89.             leftrow=((LINES-myrows)/2);
  90.             leftcol=((COLS-2)-mycols)/2;
  91.             mycols=(mycols/2);
  92.             rightrow=leftrow;
  93.             rightcol=leftcol+2+mycols;
  94.             break;
  95.    default: usage();
  96.  }
  97.  
  98.  if (!(leftwin=newwin(myrows,mycols,leftrow,leftcol))){
  99.    fprintf(stderr,"fileview: can't open dir_win\n\n");
  100.    endwin(); exit(1);
  101.  }
  102.  
  103.  if (!(rightwin=newwin(myrows,mycols,rightrow,rightcol))){
  104.    fprintf(stderr,"fileview: can't open file_win\n\n");
  105.    endwin(); exit (1);
  106.  }
  107.  
  108.  start_color();
  109.  if (has_colors()){
  110.    init_pair(1,COLOR_WHITE,COLOR_RED);
  111.    wattrset(leftwin,COLOR_PAIR(1));
  112.    wattrset(rightwin,COLOR_PAIR(1));
  113.  }
  114.  else{
  115.    wattrset(leftwin,A_REVERSE);
  116.    wattrset(rightwin,A_REVERSE);
  117.  }
  118.  
  119.  vdmerase(leftwin);
  120.  vdmerase(rightwin);
  121.  
  122.  noecho();
  123.  nodelay(stdscr,FALSE);
  124.  cbreak();
  125.  curs_set(FALSE);
  126.  keypad(stdscr,TRUE);
  127.  
  128.  mvwprintw(leftwin,1,1,"LINES: %i,  COLS: %i",LINES,COLS);
  129.  mvwprintw(leftwin,2,1,"Nlines: %i, Ncols: %i",myrows,mycols);
  130.  mvwprintw(leftwin,3,1,"Begy: %i,    Begx: %i",leftrow,leftcol);
  131.  mvwaddstr(leftwin,5,1,"Press any key to quit");
  132.  
  133.  mvwprintw(rightwin,1,1,"LINES: %i,  COLS: %i",LINES,COLS);
  134.  mvwprintw(rightwin,2,1,"Nlines: %i, Ncols: %i",myrows,mycols);
  135.  mvwprintw(rightwin,3,1,"Begy: %i,    Begx: %i",rightrow,rightcol);
  136.  mvwaddstr(rightwin,5,1,"Press any key to quit");
  137.  
  138.  wnoutrefresh(leftwin);
  139.  wnoutrefresh(rightwin);
  140.  doupdate();
  141. }
  142.  
  143.  
  144.  
  145. void end_screen()
  146. {
  147.  delwin(leftwin);
  148.  delwin(rightwin);
  149.  curs_set(TRUE);
  150.  endwin();
  151. }
  152.  
  153.  
  154.  
  155. void vdmerase (WINDOW *win)
  156. {
  157. int  y,x;
  158.  
  159.  for (y=0;y<=win->_maxy;y++)
  160.    for (x=0;x<=win->_maxx;x++)
  161.      (chtype *)win->_line[y][x]=' '|win->_attrs;
  162.  win->_curx = win->_cury = 0;
  163.  touchwin(win);
  164. return;
  165. }
  166.  
  167.  
  168. void usage()
  169. {
  170.  printf("\nusage: screen[1|2|3]\n");
  171.  printf("\n       1 = opens a window with 22 lines and 70 columns in the");
  172.  printf("\n           middle of the screen.");
  173.  printf("\n       2 = opens 2 windows with one line free on top and bottom");
  174.  printf("\n           and two columns free on the left and on the right side");
  175.  printf("\n           of the screen and two columns free between them.");
  176.  printf("\n       3 = opens two windows, where for each window the number of"); 
  177.  printf("\n           lines is 75%% from LINES and the number of columns is");
  178.  printf("\n           43%% from COLS");
  179.  printf("\n\n");
  180.  exit(0);
  181. }
  182.  
  183.  
  184.  
  185. /*
  186.  * Signal handler for SIGWINCH. When this signal is received,
  187.  * we clean up all windows (delwin), then call endwin and
  188.  * reinitialize the whole program. it's not fine but it works.
  189.  * For 'real' programs we need some better functions I think.
  190.  *
  191.  */
  192. void winsizech()
  193. {
  194.  end_screen();
  195.  init_screen();
  196.  signal(SIGWINCH,winsizech);
  197. }
  198.